package org.cubikalabs.todolist.business
{
	import com.adobe.cairngorm.business.ServiceLocator;
	
	import mx.controls.Alert;
	import mx.messaging.config.ServerConfig;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	
	import org.cubikalabs.todolist.vo.Todo;
	
	public class BusinessDelegate
	{
		private var service : Object;
		private var responder:IResponder;
		
		public function BusinessDelegate(responder:IResponder)
		{
			service = ServiceLocator.getInstance().getRemoteObject(Services.TODO_SERVICE);
			this.responder = responder;	
		}
		
		
		public function login(username:String, password:String):void
		{
			ServerConfig.getChannelSet("my-amf").login(username, password).addResponder(responder);	
		}
		
		public function logout():void
		{
			ServerConfig.getChannelSet("my-amf").logout().addResponder(responder);	
		}
		
		public function getTodoList():void
		{
			try
			{
				var token : AsyncToken = this.service.getTodoList();
				token.addResponder(responder);
			}
			catch(error:Error)
			{
				Alert.show("Error getting TodoList");	
			}
		}
		
		public function saveTodo(todo:Todo):void
		{
			try
			{
				var token : AsyncToken = this.service.saveTodo(todo);
				token.addResponder(responder);
			}
			catch(error:Error)
			{
				Alert.show("Error creating crags...");	
			}
		}
	}
}